package les.projeto;

import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class ProjetoLESActivity extends Activity {
    private static final int PICK_DATE_REQUEST = 1;
    private String modelNotes = ""; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button buttonAgenda = (Button)findViewById(R.id.buttonAgenda);
        buttonAgenda.setOnClickListener(new View.OnClickListener() {
        		 
        	@Override
        	public void onClick(View v) {
        		Calendar calendar = Calendar.getInstance();
		        int day = calendar.get(Calendar.DAY_OF_MONTH);
		        int month = calendar.get(Calendar.MONTH);
		        int year = calendar.get(Calendar.YEAR);
		        
		        Intent intent = new Intent(v.getContext(),CalendarView.class);
				intent.putExtra("date", year+"-"+month+"-"+day);
	    		startActivityForResult(intent, PICK_DATE_REQUEST);	
        	} 
        });
    
        Button buttonHelp = (Button)findViewById(R.id.Button02);
        buttonHelp.setOnClickListener(new View.OnClickListener() {
                 
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                int month = calendar.get(Calendar.MONTH);
                int year = calendar.get(Calendar.YEAR);
                
                Intent intent = new Intent(v.getContext(),HelpView.class);
                intent.putExtra("date", year+"-"+month+"-"+day);
                startActivityForResult(intent, PICK_DATE_REQUEST);  
            } 
        });
        
        Button buttonAbout = (Button)findViewById(R.id.Button03);
        buttonAbout.setOnClickListener(new View.OnClickListener() {
                 
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                int month = calendar.get(Calendar.MONTH);
                int year = calendar.get(Calendar.YEAR);
                
                Intent intent = new Intent(v.getContext(),AboutView.class);
                intent.putExtra("date", year+"-"+month+"-"+day);
                startActivityForResult(intent, PICK_DATE_REQUEST);  
            } 
        });
        
        
        Button buttonConfig = (Button)findViewById(R.id.buttonConfig);
        buttonConfig.setOnClickListener(new View.OnClickListener() {
                 
            @Override
            public void onClick(View v) {
                settingNotes();
            } 
        });
 
    }
    
    protected void settingNotes() {
        Resources st = getResources();
        final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
        helpBuilder.setTitle(st.getString(R.string.configNotes));
        helpBuilder.setMessage(st.getString(R.string.modelsNotes)); 
        helpBuilder.setMessage("\n       A+       |  90 - 100" + "\n       A         |  85 - 89" + 
        "\n       A-        |  80 - 84" + "\n       B+       |  77 - 79"+ "\n       B         |  73 - 76" + "\n       B-        |  70 - 72" +
        "\n       C+       |  65 - 69" + "\n       C         |  60 - 64" + "\n       C-        |  55 - 59" + "\n       D         |  50 - 54" +
        "\n       F          |    0 - 49");
        helpBuilder.setPositiveButton("Numeric", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  modelNotes = "numerico";
                  noteScale();
                  dialog.cancel();
              }
        });
        helpBuilder.setNeutralButton("Conceptual", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  modelNotes = "conceitual";
                  dialog.cancel();
              }
        });
        helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
              }
         });
        addModelNotesInBD(modelNotes);
        AlertDialog helpDialog = helpBuilder.create();
        helpDialog.show();
    }
    
    protected void noteScale() {
        final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
        helpBuilder.setTitle("Set the scale of notes.");
        helpBuilder.setPositiveButton("0 a 10", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
              }
        });
        helpBuilder.setNeutralButton("0 a 100", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
              }
        });
        helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
              }
         });
        //addModelNotesInBD(scale);
        AlertDialog helpDialog = helpBuilder.create();
        helpDialog.show();
    }
    
    private void addModelNotesInBD(String modelNotes) {
        BD bd = new BD();
        bd.insertModelNotes(this, modelNotes);  
    }
    
	private void share() {
	    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
	    sharingIntent.setType("text/plain");
	    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Estou utilizando o StudentA+. https://sites.google.com/site/projetoles20121/");
	    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
	    startActivity(Intent.createChooser(sharingIntent, "Share using"));
	}

    private void exportData() {
		Export e = new Export(ProjetoLESActivity.this);
        e.exportData();
	} 

	private void importData() {
		Intent trocatela = new Intent(ProjetoLESActivity.this, FileExplorer.class);
	    ProjetoLESActivity.this.startActivity(trocatela);
	    ProjetoLESActivity.this.finish();
	} 
    
    @Override
	public void onBackPressed() {
    	int pid = android.os.Process.myPid(); 
    	android.os.Process.killProcess(pid); 
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     super.onCreateOptionsMenu(menu);
     menu.add(0,0,0,"Import data");
     menu.add(1,1,1,"Export data");
     menu.add(2,2,2,"Share");
     return true;
    } 
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
     super.onOptionsItemSelected(item);
     switch (item.getItemId()) {
     	case 0:
     		importData();
     		break;
     	case 1:
     		exportData();
     		break;
     	case 2:
     	    share();
     	    break;
     }
     return false;
    } 
}